package com.engine.modules.ytht.service;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.baseEntity.gis.BoxRangeEntity;
import com.engine.common.util.CacheUtil.MyCacheUtil;
import com.engine.common.util.GeoUtil.GeomCaculateUtil;
import com.engine.common.util.ListMapAndJson;
import com.engine.init.Globle;
import com.engine.init.entity.TablePropertieEntity;
import com.engine.modules.general.mapper.GeneralDataCommonMapper;
import com.engine.modules.vector.mapper.VectorSourceMapper;
import com.engine.modules.vector.mapper.VectorTilesMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FY
 * @version 1.0.0
 * @ClassName TobaccoRegionalPlanningImpl.java
 * @Description TODO
 * @createTime 2022年01月11日
 */
@Service
@Slf4j
public class TobaccoRegionalPlanningImpl implements TobaccoRegionalPlanning {

    private static Map<String, Object> TobaccoRegionalCachePacMap = new HashMap<>();

    @Autowired
    VectorTilesMapper vectorTilesMapper;

    @Autowired
    VectorSourceMapper vectorSourceMapper;

    @Autowired
    GeneralDataCommonMapper generalDataCommonMapper;

    @Autowired
    CacheManager cacheManager;

    @Override
    @Cacheable(value = "vectorTilesCache", key = "'TobaccoRegional:ut:'+#pac+':'+#z+':'+#x+':'+#y")
    public byte[] selectTobaccoRegionalVectorTileByXYZ(int z, int x, int y, String pac) {
        String tableName = "b_gis_polygon_ytxx";
        if (z <= 14)
            tableName = "b_gis_polygon_ytxx_lv14";
        if (z <= 12)
            tableName = "b_gis_polygon_ytxx_lv12";
        if (z <= 10)
            tableName = "b_gis_polygon_ytxx_lv10";
        if (z <= 8)
            tableName = "b_gis_polygon_ytxx_lv8";
        if (pac.length() > 6)
            pac = pac.substring(0, 5);
        byte[] bytes = vectorTilesMapper.selectTileByXYZ(z, x, y, tableName,
                "TobaccoRegional", pac);
        return bytes;
    }

    @Override
    @Cacheable(value = "geoJsonCache", key = "'TobaccoRegional:gid:'+#pac+':'+#id")
    public Map<String, Object> selectTobaccoRegionalGeojsonById(String id, String pac) {
        List<String> ids = new ArrayList<>();
        ids.add(id);
        List<Map<String, Object>> mapList = vectorSourceMapper.selectGeojsonByIds(ids, "b_gis_polygon_ytxx", pac);
        if (mapList.size() > 0)
            return mapList.get(0);
        else
            return null;
    }

    @Override
    public List<String> insertTobaccoRegionalByGeojson(String geojson, String pac) {
        List<String> mapList = vectorSourceMapper.insertByGeojson(geojson, "b_gis_polygon_ytxx");
        if (mapList.size() > 0) {
            for (String id:mapList) {
                rmVectorCacheById(cacheManager,"b_gis_polygon_ytxx",id,pac);
            }
            return mapList;
        } else
            return null;
    }

    @Override
    public List<String> updateTobaccoRegionalByGeojson(String geojson, String pac) {
        List<String> mapList = vectorSourceMapper.updateByGeojson(geojson,"b_gis_polygon_ytxx",pac);
        if (mapList.size() > 0) {
            for (String id:mapList) {
                rmVectorCacheById(cacheManager,"b_gis_polygon_ytxx",id,pac);
            }
            return mapList;
        } else
            return null;
    }

    @Override
    public String deleteTobaccoRegionalByIdLogic(String id, String pac) {
        List<String> ids = new ArrayList<>();
        ids.add(id);
        List<String> mapList = vectorSourceMapper.deleteLogicByIds(ids, "b_gis_polygon_ytxx", pac);
        if (mapList.size() > 0)
        {
            String iid=mapList.get(0);
            rmVectorCacheById(cacheManager,"b_gis_polygon_ytxx",iid,pac);
            return iid;
        }
        else
            return "";
    }

    @Override
    public String deleteTobaccoRegionalById(String id, String pac) {
        String iid=deleteTobaccoRegionalByIdLogic(id,pac);
        if (!iid.equals(""))
        {
            rmVectorCacheById(cacheManager,"b_gis_polygon_ytxx",iid,pac);
            List<String> ids = new ArrayList<>();
            ids.add(id);
            List<String> mapList=vectorSourceMapper.deleteByIds(ids,"b_gis_polygon_ytxx", pac);
            if (mapList.size() > 0)
                return mapList.get(0);
            else
                return null;
        }
        return "";
    }

    //清除矢量切片和geojson的缓存
    public void rmVectorCacheById(CacheManager cacheManager, String tableName, String id, String pac) {
        rmVectorTileCacheById(cacheManager, tableName, "TobaccoRegional:ut:", id, pac, 6, 20);
        MyCacheUtil.rmBykey(cacheManager, "geoJsonCache", "TobaccoRegional:gid:", id);
    }

    public void rmVectorTileCacheById(CacheManager cacheManager, String tableName, String keyFirstHalf, String id, String pac, int minz, int maxz) {
        BoxRangeEntity boxRangeEntity = generalDataCommonMapper.selectBoxRangeById(tableName, id);
        if (boxRangeEntity == null)
            return;
        List<String> stringList = GeomCaculateUtil.LngLatRangeToTileNumListStringWithPac(minz, maxz, pac, boxRangeEntity);
        MyCacheUtil.rmBykeyList(cacheManager, "vectorTilesCache", keyFirstHalf, stringList);
    }
}
